package com.itcast.tanhuaserver.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.tanhua.dubbo.server.api.QuanZiApi;
import com.itcast.tanhua.dubbo.server.api.VideoApi;
import com.itcast.tanhua.dubbo.server.domain.Video;
import com.itcast.tanhuaserver.domain.User;
import com.itcast.tanhuaserver.domain.UserThreadLocal;
import com.itcast.tanhuaserver.service.VideoMQService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuaserver.service.impl
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2519:22
 * @Version 1.0
 */
@Service
@Slf4j
public class VideoMQServiceImpl implements VideoMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference(version = "1.0.0")
    private VideoApi videoApi;
    /**
     * 发送小视频操作相关的消息
     *
     * @param videoId
     * @param type     1-发动态，2-点赞， 3-取消点赞，4-评论
     * @return
     */
    private Boolean sendMsg(String videoId, Integer type){

        try {
            User user = UserThreadLocal.get();
            Video video = this.videoApi.queryVideoById(videoId);

            Map<String,Object> msg = new HashMap<>();
            msg.put("userId",user.getId());
            msg.put("date",System.currentTimeMillis());
            msg.put("videoId",videoId);
            msg.put("vid",video.getVid());
            msg.put("type",type);
            this.rocketMQTemplate.convertAndSend("tanhua-video",msg);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("小视频发送消息失败~ videoId=" + videoId + ", type=" + type,e);
            return false;
        }
        return true;
    }

    @Override
    public Boolean videoMsg(String videoId) {
        return null;
    }

    @Override
    public Boolean likeVideoMsg(String videoId) {
        return null;
    }

    @Override
    public Boolean disLikeVideoMsg(String videoId) {
        return null;
    }

    @Override
    public Boolean commentVideoMsg(String videoId) {
        return null;
    }
}
